package com.xiaoyy.app.extend.scheduling;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sun.org.apache.xpath.internal.operations.Bool;
import com.xiaoyy.app.extend.common.enums.AppApiEnum;
import com.xiaoyy.core.common.enums.BoolEnum;
import com.xiaoyy.core.common.exception.AppException;
import com.xiaoyy.core.common.vo.AppResponse;
import com.xiaoyy.core.common.vo.TokenVO;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 数据同步定时任务
 * */
//@ConditionalOnExpression("${system.config.srcm.sync.flag}")
//@Component
public class SyncTalentTask {
    protected Logger logger = LoggerFactory.getLogger(this.getClass());
    @Value("${system.config.srcm.url}")
    private String srcmUrl;
    @Value("${system.config.jhrc.app.url}")
    private String appUrl;
    /**
     * 上次执行完后5分钟再执行
     *
     * */
    @Scheduled(fixedDelay = 300000)
    public void sync(){
        logger.info("省人才库数据同步开始");
        this.syncHttp();
        logger.info("省人才库数据同步结束");
    }

    private void syncHttp(){
        String syncBody= "";
        JSONObject srcmJson = null;
        Long talentId=null;
        try {
            syncBody = HttpUtil.post(appUrl + AppApiEnum.RCM0001.getType(), JSON.toJSONString(new HashMap<>()));
        }catch (Exception e){
            throw new AppException("调用获取同步数据接口异常"+e.getMessage());
        }
        if (StringUtils.isNotBlank(syncBody)) {
            AppResponse appResponse = JSON.parseObject(syncBody, AppResponse.class);
            if (appResponse.getCode()==0){
                Object obj=appResponse.getResult();
                JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(obj));
                srcmJson = jsonObject.getJSONObject("syncTalentVO");
                talentId=jsonObject.getLong("talentId");
            }else{
                if (StringUtils.isBlank(appResponse.getMessage())){
                    throw new AppException(syncBody);
                }
                throw new AppException(appResponse.getMessage());
            }
        }else{
            throw new AppException("未返回同步数据");
        }
        if(talentId==null){
            return;
        }
        if(srcmJson==null){
            throw new AppException("未返回人才数据");
        }
        String url = srcmUrl+"/city_data";
        //String url="http://116.63.56.139/zju_telent/public/city_data";
        String jgBody="";
        try {
            jgBody = HttpUtil.post(url, JSON.toJSONString(srcmJson));
        }catch (Exception e){
            throw new AppException("调用省同步数据接口异常"+e.getMessage());
        }
        if (StringUtils.isNotBlank(jgBody)) {
            JSONObject jsonObject =null;
            try{
                jsonObject = JSON.parseObject(jgBody);
            }catch (Exception e){
                throw new AppException("省人才码返回信息异常："+jgBody);
            }
            Integer state=jsonObject.getInteger("state");
            Map<String,Object> map = new HashMap<>();
            map.put("talentId", talentId);
            if (state!=null&&state==1){
                map.put("syncFlag", BoolEnum.TRUE.getType());
                map.put("syncMsg", "成功");
            }else{
                //失败
                String msg = jsonObject.getString("msg");
                map.put("syncFlag", "9");
                map.put("syncMsg", msg);
            }
            HttpUtil.post(appUrl + AppApiEnum.RCM0002.getType(), JSON.toJSONString(map));
        }else{
            throw new AppException("省里未返回结果数据");
        }
        this.syncHttp();
    }


}
